import { AsyncStorage } from 'react-native';
import DeviceInfo from 'react-native-device-info';
import moment from 'moment';

export function setPrinterAddress(address) {
  return AsyncStorage.setItem('PRINTER', address);
}

export function getPrinterAddess() {
  return AsyncStorage.getItem('PRINTER');
}

export function getServiceId() {
  return AsyncStorage.getItem('PRINT_SERVICEID');
}

export function setServiceId(serviceId) {
  return AsyncStorage.setItem('PRINT_SERVICEID', serviceId);
}

export function getCharacterId() {
  return AsyncStorage.getItem('PRINT_CHARACTERID');
}

export function setCharacterId(characterId) {
  return AsyncStorage.setItem('PRINT_CHARACTERID', characterId);
}

export function clearStorage() {
  return AsyncStorage.clear();
}

export function setPickupCalenderId(orderId, eventId) {
  console.log(`setPickupCalenderId: ${orderId} ${eventId}`);
  return AsyncStorage.setItem(`${orderId}PICKUP`, eventId);
}

export function getPickupCalenderId(orderId) {
  console.log(`getPickupCalenderId: ${orderId}`);
  return AsyncStorage.getItem(`${orderId}PICKUP`);
}

export function setDeliveryCalenderId(orderId, eventId) {
  console.log(`setDeliveryCalenderId: ${orderId} ${eventId}`);
  return AsyncStorage.setItem(`${orderId}DELIVERY`, eventId);
}

export function getDeliveryCalenderId(orderId) {
  console.log(`getDeliveryCalenderId: ${orderId}`);
  return AsyncStorage.getItem(`${orderId}DELIVERY`);
}

export function setNewOrderItems(premium, items) {
  return AsyncStorage.setItem(`${premium?'PREMIUM':''}ORDERCACHE_ITEM`, items?JSON.stringify(items):null);
}

export function getNewOrderItems(premium) {
  const key = `${premium?'PREMIUM':''}ORDERCACHE_ITEM`;
  return AsyncStorage.getItem(key)
    .then(itmes => {
      const result = itmes?JSON.parse(itmes):null;
      if (!(result instanceof Array)) {
        AsyncStorage.removeItem(key);
        return null;
      }

      return result;
    })
}

export function clearNewOrderItems(premium) {
  return AsyncStorage.removeItem(`${premium?'PREMIUM':''}ORDERCACHE_ITEM`);
}

export function displayInChinese() {
  return AsyncStorage.getItem('DEVICE_LAUNGUAGE')
    .then(launguage => {
      if (launguage&&launguage.indexOf('zh')===0) {
        return true;
      } else {
        return false;
      }
    })
}

export function setReadLaundryGuide(premium) {
  return AsyncStorage.setItem(`${premium?'PREMIUM_':''}LAUNDRY_GUIDE_READED`, 'YES');
}

export function isLaundryGuideRead(premium) {
  return AsyncStorage.getItem(`${premium?'PREMIUM_':''}LAUNDRY_GUIDE_READED`)
    .then(read => read?true:false);
}

export function setDefaultAddress(address) {
  if (address) {
    return AsyncStorage.setItem('DEFAULT_ADDRESS', JSON.stringify(address));
  } else {
    return AsyncStorage.removeItem('DEFAULT_ADDRESS');
  }
}

export function getDefaultAddress() {
  return AsyncStorage.getItem('DEFAULT_ADDRESS')
    .then(address => address&&JSON.parse(address));
}

export function setDefaultAddressPrompt() {
  return AsyncStorage.setItem('FIRSTTIME_DEFAULT_ADDRESS', 'YES');
}

export function promotForDefaultAddress() {
  return AsyncStorage.getItem('FIRSTTIME_DEFAULT_ADDRESS')
    .then(read => {
      return getDefaultAddress()
        .then(address => {
          return (!read&&!address)?true:false;
        });
    });
}

export function getDeviceInfo() {
  return getPushId()
    .then(pushId => ({
      appVersion: DeviceInfo.getVersion(),
      platform: DeviceInfo.getSystemName(),
      platformVersion: DeviceInfo.getSystemVersion(),
      deviceModel: `${DeviceInfo.getManufacturer()||''} ${DeviceInfo.getModel()||''}`,
      deviceId: DeviceInfo.getUniqueID(),
      pushId
    }));
}

export function getLoginToken() {
  return AsyncStorage.getItem('KKTOKEN');
}

export function setLoginToken(token) {
  return AsyncStorage.setItem('KKTOKEN', token);
}

export function clearLoginToken() {
  return AsyncStorage.removeItem('KKTOKEN');
}

export function getPushId() {
  return AsyncStorage.getItem('PUSHDEVICEID');
}

export function setPushId(deviceId) {
  return AsyncStorage.setItem('PUSHDEVICEID', deviceId);
}

export function ignoreVersion() {
  return AsyncStorage.setItem('IGNORETIME', new Date().toString());
}

export function ignoreUpdate() {
  return AsyncStorage.getItem('IGNORETIME')
    .then(date => {
      return !date||(moment(date).diff(moment(), 'days')<-7);
    });
}

export function getReviewDate() {
  return AsyncStorage.getItem('REVIEW_DATE');
}

export function setReviewDate() {
  return AsyncStorage.setItem('REVIEW_DATE', new Date().toString());
}

// Old

export function setLastOrderAddressId(addressId) {
  return AsyncStorage.setItem('LASTADDRESSID', addressId);
}

export function getLastOrderAddressId() {
  return AsyncStorage.getItem('LASTADDRESSID');
}

export function setOrderPrompt(message) {
  return AsyncStorage.setItem('ORDERPROMPT', message);
}

export function getOrderPrompt() {
  return AsyncStorage.getItem('ORDERPROMPT');
}

export function setGalleryImages(images) {
  return AsyncStorage.setItem('GALLERY_IMAGES', images&&JSON.stringify(images));
}

export function getGalleryImages() {
  return AsyncStorage.getItem('GALLERY_IMAGES')
    .then(images => images&&JSON.parse(images));
}

export function setPickupReminder(reminder) {
  return AsyncStorage.setItem('PICKUP_REMINDER', reminder?null:'FALSE');
}

export function getPickupReminder() {
  return (AsyncStorage.getItem('PICKUP_REMINDER')==='FALSE')?false:true;
}

export function setDropOffReminder(reminder) {
  return AsyncStorage.setItem('DROPOFF_REMINDER', reminder?null:'FALSE');
}

export function getDropOffReminder() {
  return (AsyncStorage.getItem('DROPOFF_REMINDER')==='FALSE')?false:true;
}

export function setPickupReminderOrderId(orderId) {
  return AsyncStorage.setItem('PICKUP_REMINDER_ORDERID', orderId);
}

export function getPickupReminderOrderId(orderId) {
  return AsyncStorage.getItem('PICKUP_REMINDER_ORDERID')
    .then(savedOrderId => {
      if (savedOrderId === orderId) {
        AsyncStorage.setItem('PICKUP_REMINDER_ORDERID', null);
        return orderId;
      }
      return null;
    })
}

export function setDropOffReminderOrderId(orderId) {
  return AsyncStorage.setItem('DROPOFF_REMINDER_ORDERID', orderId);
}

export function getDropOffReminderOrderId(orderId) {
  return AsyncStorage.getItem('DROPOFF_REMINDER_ORDERID')
    .then(savedOrderId => {
      if (savedOrderId === orderId) {
        AsyncStorage.setItem('DROPOFF_REMINDER_ORDERID', null);
        return orderId;
      }
      return null;
    })
}

export function settingSetDefaultSkipItem(skip) {
  if (skip) {
    return AsyncStorage.setItem('DEFAULT_SKIP_ITEM', 'YES');
  } else {
    return AsyncStorage.removeItem('DEFAULT_SKIP_ITEM');
  }
}

export function settingGetDefaultSkipItem() {
  return AsyncStorage.getItem('DEFAULT_SKIP_ITEM')
    .then(set => set?true:false);
}

